package com.winz.cultureapp.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.winz.cultureapp.utils.MyToast;
import com.winz.cultureapp.widge.CustomProgressDialog;

import butterknife.ButterKnife;


/**
 * Fragment的基类，项目中所有Fragment应该继承于该类
 */
public abstract class BaseFragment extends Fragment {

    /**
     * Fragment当前状态是否可见
     *
     */
    protected boolean isVisible;

    public Activity mContext;
    public Resources mResources;

    protected MyToast toast;

    public CustomProgressDialog pd = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (mContext == null) {
            mContext = activity;
            toast = new MyToast(activity);
            mResources = activity.getResources();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(contentView(), container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    /**
     * 该方法不属于Fragment的生命周期方法
     * 该方法会紧跟着onCreateView方法被调用
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        initViews(view);
    }

    /**
     * 事件处理方法
     */
    protected void initEvents(){

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDatas(getArguments());

        initEvents();
        loadDatas();
    }

    /**
     * 解析获得的数据
     * @param arguments
     */
    protected void getDatas(Bundle arguments) {

    }


    /**
     * 子类调用该方法，返回fragment中需要显示的布局ID
     * @return
     */
    protected abstract int contentView();

    /**
     * 初始化Fragment，由子类具体实现
     * @param view
     */
    protected void initViews(View view) {}

    /**
     * 加载数据方法，由子类具体实现
     */
    protected void loadDatas() {}


    public void startActivityClearTop(Class<?> activity){
        Intent intent = new Intent(mContext, activity);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }


    public void startActivityNewTask(Class<?> activity){
        Intent intent = new Intent(mContext, activity);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    public void startActivity(Class<?> activity,boolean isExit){
        startActivity(activity, isExit, null);
    }

    public void startActivity(Class<?> activity,boolean isExit,Bundle bundle){
        startActivity(activity, isExit, bundle, -1);
    }

    public void startActivity(Class<?> activity, boolean isExit, int requestCode){
        startActivity(activity, isExit, null, requestCode);
    }

    public void startActivity(Class<?> activity,boolean isExit,Bundle bundle, int requestCode){

        Intent intent = new Intent(mContext, activity);
        if(bundle!=null){
            intent.putExtras(bundle);
        }

        startActivityForResult(intent, requestCode);

        if(isExit)
            mContext.finish();
    }

    public Bundle getBundle() {
        return new Bundle();
    }

    public void showMsg(String content) {
        if (content != null){
            toast.show(content,10);
        }
    }

    public void showMsg(int content) {
        toast.show(content,10);
    }

    public String getResourceStr(int id) {
        return mResources.getString(id);
    }

    public String getResourceStr(int id, String args) {
        return mResources.getString(id, args);
    }

    public String getResourceStr(int id, int args) {
        return mResources.getString(id, args);
    }


    @Override
    public void onPause() {
        super.onPause();
        toast.cancel();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }
    /**
     * 不可见
     */
    protected void onInvisible() {


    }
    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected  void lazyLoad(){

    }


    /**
     * 显示加载框
     */
    public void showProgressDialog(String msg) {
        if (!mContext.isFinishing()){
            if (pd == null) {
                pd = CustomProgressDialog.createDialog(mContext);
            }
            pd.setMessage(msg);
            pd.setCanceledOnTouchOutside(true);
            pd.show();
        }
    }

    /**
     * 显示加载框
     */
    public void showProgressDialog() {
        if (!mContext.isFinishing()) {
            if (pd == null) {
                pd = CustomProgressDialog.createDialog(mContext);
            }
            pd.setMessage("努力为您加载中...");
            pd.setCanceledOnTouchOutside(true);
            pd.show();
        }
    }

    /**
     * 取消加载框
     */
    public void dismissProgressDialog() {
        if (!mContext.isFinishing()) {
            if (pd != null) {
                pd.dismiss();
                pd = null;
            }
        }
    }
}
